package com.zmn.oms.business.impl.log;

import com.alibaba.fastjson.JSON;
import com.zmn.base.engineer.common.constants.DubboConsts;
import com.zmn.base.engineer.common.dro.foreign.engineer.ForeignEngineerBasicInfoDRO;
import com.zmn.base.engineer.dubbo.interfaces.foreign.EngineerListRemoteService;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.oms.business.interfaces.log.OrderQuotationLogBService;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.constant.OrderLogConsts;
import com.zmn.oms.common.constant.OrderStatusConsts;
import com.zmn.oms.model.entity.log.OrderLog;
import com.zmn.oms.model.entity.log.OrderQuotationLog;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 类描述：
 *
 * @author lhl
 * @date 2020/07/07 11:36
 */
@Slf4j
@Service("orderQuotationLogBService")
public class OrderQuotationLogBServiceImpl implements OrderQuotationLogBService {


    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    OrderWorkService orderWorkService;
    //@Reference(version = com.zmn.base.engineer.common.constants.DubboConsts.INTERFACE_VERSION, check = false)
    //private BaseInfoListRemoteService baseInfoListRemoteService;

    @Reference(version = DubboConsts.INTERFACE_VERSION, check = false)
    private EngineerListRemoteService engineerListRemoteService;

    @Override
    public void save(OrderQuotationLog orderQuotationLog) {
        mongoTemplate.save(orderQuotationLog);
    }

    @Override
    public void saveLogWithOrderLog(OrderQuotationLog orderQuotationLog) {
        Assert.notNull(orderQuotationLog.getType(), "日志类型不能为空");
        OrderWork orderWork = orderWorkService.findOrderWorkByKeySrcMaster(orderQuotationLog.getOrderId(), orderQuotationLog.getWorkId());
        orderQuotationLog.setCreateTime(new Date());
        if (orderQuotationLog.getOperatorType().equals(GlobalConsts.OPERATE_USER_TYPE_MASTER)) {
            orderQuotationLog.setUserId((long)orderWork.getMasterId());
        }
        mongoTemplate.save(orderQuotationLog);
        this.addOrderLog(orderQuotationLog,orderWork);
    }

    private void addOrderLog(OrderQuotationLog orderQuotationLog,OrderWork orderWork){
        if (Objects.equals(orderQuotationLog.getType(), OrderConsts.QUOTATION_START)) {
            OrderLog orderLog = new OrderLog();
            orderLog.setCreateTime(new Date());
            orderLog.setOrderId(orderQuotationLog.getOrderId());
            orderLog.setWorkId(orderQuotationLog.getWorkId());
            orderLog.setOperatorType(orderQuotationLog.getOperatorType());
            orderLog.setOperatorTypeName(GlobalConsts.getOperatorTypeName(orderQuotationLog.getOperatorType()));
            if (orderQuotationLog.getOperatorType().equals(GlobalConsts.OPERATE_USER_TYPE_USER)) {
                orderLog.setOperator("用户");
                orderLog.setOperatorId(orderWork.getUserId());
            }else {
                // todo fubiao
                log.info("addOrderLog#getEngineerById start [{}]",orderWork.getMasterId());
                ResponseDTO<ForeignEngineerBasicInfoDRO> basicInfo = engineerListRemoteService.getEngineerById(orderWork.getMasterId());
                log.info("addOrderLog#getEngineerById end [{}]", JSON.toJSONString(basicInfo));
                orderLog.setOperator(basicInfo.getData().getRealName());
                orderLog.setOperatorId(orderWork.getMasterId().longValue());
            }
            orderLog.setMasterId(orderWork.getMasterId());
            orderLog.setPlat(orderWork.getPlat());
            orderLog.setPlatWork(orderWork.getPlatWork());
            orderLog.setManageCompanyId(orderWork.getManageCompanyId());

            // 操作前状态结果
            Integer status = orderWork.getStatus();
            Integer resultStatus = orderWork.getResultStatus();

            String orderStatusName = OrderStatusConsts.getWorkStatusName(status);
            String orderResultStatusName = OrderStatusConsts.getWorkResultStatusName(resultStatus);
            orderLog.setBeforeStatus(status);
            orderLog.setBeforeStatusName(orderStatusName);
            orderLog.setBeforeResultStatus(resultStatus);
            orderLog.setBeforeResultStatusName(orderResultStatusName);

            orderLog.setAfterStatus(status);
            orderLog.setAfterStatusName(orderStatusName);
            orderLog.setAfterResultStatus(resultStatus);
            orderLog.setAfterResultStatusName(orderResultStatusName);

            StringBuilder sb = new StringBuilder();
            orderLog.setType(OrderLogConsts.ORDER_LOG_TYPE_QUOTATION_START);
            orderLog.setTypeName(OrderLogConsts.getOrderLogTypeName(OrderLogConsts.ORDER_LOG_TYPE_QUOTATION_START));

            if (orderQuotationLog.getOperatorType().equals(GlobalConsts.OPERATE_USER_TYPE_USER)) {
                sb.append("用户打开计价器：");
            }else{
                sb.append("工程师打开计价器：");
            }
            sb.append(orderQuotationLog.getStartTime());

            orderLog.setContent(sb.toString());
            mongoTemplate.save(orderLog);
        }
    }

    @Override
    public List<OrderQuotationLog> listByOrderIdAndWorkId(Long orderId, Long workId) {
        Query query = new Query();
        Criteria criteria = new Criteria();
        criteria.and("orderId").is(orderId);
        criteria.and("workId").is(workId);
        query.addCriteria(criteria);
        query.with(Sort.by(Sort.Order.desc("beginTime")));
        return mongoTemplate.find(query, OrderQuotationLog.class);
    }

}
